
//
//  MechanicalViewController.m
//  PASystem
//
//  Created by 刘沿 on 16/8/19.
//  Copyright © 2016年 czd. All rights reserved.
//

#import "MechanicalViewController.h"
#import "MechanicalModel.h"
#import "MechanicalTableViewCell.h"
#import "MechanicalDetailController.h"


@interface MechanicalViewController ()

@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic,assign) NSInteger currentPage;

@end

@implementation MechanicalViewController

static NSString *const ID = @"MechanicalTableViewCell";

-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        
        _dataArray = [NSMutableArray array];
    }
    
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = _string;
         self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

    self.tableView.backgroundColor = [UIColor whiteColor];
    
    __weak typeof (self) weakSelf = self;
    
    [self.tableView addHeaderWithCallback:^{
        
        [weakSelf loadData]; //加载网络数据
    }];
    [self.tableView headerBeginRefreshing];
    
    [self.tableView addFooterWithCallback:^{
        
        [weakSelf MoreData];
        
    }];
    
     UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search"]  style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction)];
        [self.navigationItem setRightBarButtonItem:rightButton];

    
}

- (void)rightButtonAction{

chaxunjwViewController *vc  = [[chaxunjwViewController alloc]init];
//vc.type = _type;
  if (self.type == zhejiang) {
        _ty = 2;
    }else if(self.type == zhejiangTaiWan){
        _ty = 3;
    }
vc.type = _ty;
[self.navigationController pushViewController:vc animated:YES];


}
- (void)loadData{
    
    NSInteger ty = 0;
    
    if (self.type == zhejiang) {
        ty = 2;
    }else if(self.type == zhejiangTaiWan){
        ty = 3;
    }
    
    self.currentPage = 1;
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json = [NSString stringWithFormat:@"{'action': dxmList,'service': DXMService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,pageNum:%ld,type:%ld,name:%@,endtime:%@,begintime:%@}'}",userId,pwdId,userHumber,self.currentPage,ty,_name,_timeafterstring,_timebeforestring];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        [self.dataArray removeAllObjects];
        NSArray *arr = JSON[@"reason"];
        for (NSDictionary *dict in arr) {
            
            NSDictionary *dict1 = dict[@"attrs"];
            MechanicalModel *model = [MechanicalModel mj_objectWithKeyValues:dict1];
        
            [self.dataArray addObject:model];
            
        }
        
        [self.tableView headerEndRefreshing];
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
        
    }];
    
    
}

- (void)MoreData{
    
    NSInteger ty = 0;
    
    if (self.type == zhejiang) {
        ty = 2;
    }else{
        ty = 3;
    }
    
    self.currentPage ++ ;
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json = [NSString stringWithFormat:@"{'action': dxmList,'service': DXMService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,pageNum:%ld,type:%ld,name:%@,endtime:%@,begintime:%@}'}",userId,pwdId,userHumber,self.currentPage,ty,_name,_timeafterstring,_timebeforestring];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        NSArray *arr = JSON[@"reason"];
        for (NSDictionary *dict in arr) {
            
            NSDictionary *dict1 = dict[@"attrs"];
            MechanicalModel *model = [MechanicalModel mj_objectWithKeyValues:dict1];
            
            [self.dataArray addObject:model];
            
        }
        
        [self.tableView footerEndRefreshing];
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
        
    }];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    MechanicalModel *model = self.dataArray[indexPath.row];
    
    MechanicalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    if (cell == nil) {
        
        cell = [MechanicalTableViewCell WithMechanicalTableViewCell];
    }
    
    cell.model = model;
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MechanicalModel *model = self.dataArray[indexPath.row];
    
    MechanicalDetailController *dVC = [[MechanicalDetailController alloc] init];
    
    dVC.ID = model.ID;
    
    [self.navigationController pushViewController:dVC animated:YES];
    
}








@end
